package com.emep.changzhi.analyse.http.basics;

import com.emep.changzhi.analyse.entity.basics.SysDepartmentInfo;
import com.emep.changzhi.analyse.model.ResultBean;
import com.emep.changzhi.analyse.service.basics.DepartmentService;
import com.emep.changzhi.analyse.utils.Constant;
import com.emep.changzhi.analyse.utils.DataValidator;
import com.emep.changzhi.analyse.utils.MapToObject;
import com.emep.changzhi.analyse.utils.PageChunk;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Map;

/**
 * @author: LFY
 * @version:V1.0
 * @Description:
 * @Date: 2018/9/3 15:45
 */
@Api(value = "环保局部门模块")
@RestController
@RequestMapping(value = "/department")
public class DepartmentController {
    @Autowired
    DepartmentService departmentService;
    MapToObject<SysDepartmentInfo> mapToObject=new MapToObject<>();

    @ApiOperation(value = "查询环保局列表", notes = "传入若干参数")
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    @ResponseBody
    public ResultBean list(@RequestBody Map<String, Object> payload) {
        ResultBean result = DataValidator.validateMapData(payload, "pageIndex", "pageSize", "condition");
        if (result.getCode() == Constant.RESULT_CODE_SUCCESS) {
            int pageIndex = Integer.parseInt(payload.get("pageIndex").toString());
            int pageSize = Integer.parseInt(payload.get("pageSize").toString());
            Map<String, Object> condition = (Map<String, Object>) payload.get("condition");
            Page<SysDepartmentInfo> departmentList = departmentService.find(pageIndex, pageSize, condition);
            PageChunk<SysDepartmentInfo> pageChunk = new PageChunk<>();
            if(departmentList != null){
                return result.boolAdd(pageChunk.pageChunk(departmentList));
            }else {
                return result.error("未查到部门列表");
            }
        }
        return result;
    }

    @ApiOperation(value = "查询环保局部门详情", notes = "传入若干参数")
    @RequestMapping(value = "/info", method = RequestMethod.POST)
    @ResponseBody
    public ResultBean info(@RequestBody Map<String, Object> payload) {
        ResultBean result = DataValidator.validateMapData(payload, "departmentId");
        if (result.getCode() == Constant.RESULT_CODE_SUCCESS) {
            Long departmentId = Long.parseLong(payload.get("departmentId").toString());
            SysDepartmentInfo department = departmentService.getDepartment(departmentId);
            if(department != null){
                return result.boolAdd(department);
            }else {
                return result.error("无此部门");
            }
        }
        return result;
    }

    @ApiOperation(value = "更新环保局部门信息", notes = "传入若干参数")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ResponseBody
    public ResultBean update(@RequestBody Map<String, Object> payload) {
        ResultBean result = DataValidator.validateMapData(payload, "department");
        if (result.getCode() == Constant.RESULT_CODE_SUCCESS) {
            Map<String, Object> map = (Map<String, Object>) payload.get("department");
                Long departmentId = Long.parseLong(map.get("departmentId").toString());
                if(departmentId!=null) {
                    SysDepartmentInfo department = departmentService.getDepartment(departmentId);
                    boolean b = mapToObject.convertEntity(department, map);
                    if (department != null) {
                        if (b) {
                            boolean a = departmentService.updateDepartment(department);
                            if (a) {
                                return result.boolAdd("修改成功");
                            } else {
                                return result.boolAdd("该名称已存在");
                            }
                        } else {
                            return result.error("参数错误");
                        }
                    } else {
                        return result.boolAdd("无此部门");
                    }
                }else {
                    return result.error("departmentId为必填参数");
                }
        }
        return result;
    }

    @ApiOperation(value = "添加部门", notes = "传入若干参数")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ResponseBody
    public ResultBean add(@RequestBody Map<String, Object> payload) {
        ResultBean result = DataValidator.validateMapData(payload, "department");
        if (result.getCode() == Constant.RESULT_CODE_SUCCESS) {
            Map<String, Object> map = (Map<String, Object>) payload.get("department");
            SysDepartmentInfo department=new SysDepartmentInfo();
           boolean b= mapToObject.convertEntity(department, map);
           if (b){
               boolean flag= departmentService.add(department);
               if(flag){
                   return result.boolAdd("新增成功");
               }else {
                   return result.error("该名称已存在");
               }
           }else {
               return result.error("参数失败");
           }

        }
        return result;
    }

    @ApiOperation(value = "删除环保局部门信息", notes = "传入若干参数")
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ResponseBody
    public ResultBean delete(@RequestBody Map<String, Object> payload) {
        ResultBean result = DataValidator.validateMapData(payload, "departmentId");
        if (result.getCode() == Constant.RESULT_CODE_SUCCESS) {
            Long departmentId = Long.parseLong(payload.get("departmentId").toString());
            if(departmentId!=null) {
                SysDepartmentInfo department = departmentService.getDepartment(departmentId);
                if (department != null) {
                    if(!department.getIsDelete().equals(Constant.RESULT_DELETECODE_YES)){
                        department.setIsDelete(Constant.RESULT_DELETECODE_YES);
                        boolean a = departmentService.deleteDepartment(department);
                        if (a) {
                            return result.boolAdd("删除成功");
                        } else {
                            return result.boolAdd("删除失败");
                        }
                    }else {
                        return result.error("此部门状态为已删除,删除失败");
                    }
                } else {
                    return result.boolAdd("无此部门");
                }
            }else {
                return result.error("departmentId为必填参数");
            }
        }
        return result;
    }


}
